//
//  BLPublishViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/23.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLPublishViewController.h"
#import <POP.h>
#import "BLVerticalButton.h"
@interface BLPublishViewController ()

@end


@implementation BLPublishViewController

- (instancetype)init
{
    if (self = [super init]) {
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        
    //顶部文字
    UIImageView *topTiltImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
    topTiltImageView.center = CGPointMake(mainW * 0.5, mainH * 0.2 - mainH);
    [self.view addSubview:topTiltImageView];
    
    POPSpringAnimation *topAnim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    topAnim.fromValue = [NSValue valueWithCGPoint:CGPointMake(mainW * 0.5, - mainH * 0.05 ) ];
    topAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(mainW * 0.5, mainH * 0.2)];
    topAnim.springBounciness = 10;
    topAnim.springSpeed = 10;
    topAnim.beginTime = CACurrentMediaTime() + 0.05 * 6;
    [topTiltImageView pop_addAnimation:topAnim forKey:nil];
    
    //中间六个按钮
    NSArray *title = @[@"发视频",@"发图片",@"发段子",@"发声音",@"审贴",@"发链接"];
    NSArray *btnImage = @[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
    
    NSArray *time = @[ @0.25,@0.15,@0.20,@0.1,@0,@0.05];
    
    CGFloat btnW = 72;
    CGFloat btnH = 90;
    CGFloat beginX = 20;
    CGFloat beginY = (mainH - 2 * btnH)/2;
    CGFloat marginX = (mainW - beginX*2 - title.count/2 * btnW)/(title.count/2 -1);
    CGFloat marginY = 20;
    for(int i = 0;i < title.count; i++)
    {
        BLVerticalButton *btn = [[BLVerticalButton alloc]init];
        [btn setImage:[UIImage imageNamed:btnImage[i]] forState:UIControlStateNormal];
        [btn setTitle:title[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        
        CGFloat btnX = beginX + (btnW + marginX) * (i%3);
        CGFloat btnY = beginY + (btnH + marginY) * ((int)(i/3));
       
        //btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [self.view addSubview:btn];
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue =[NSValue valueWithCGRect:CGRectMake(btnX, btnY - mainH, btnW, btnH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(btnX, btnY , btnW, btnH)];
        anim.springBounciness = 2;
        anim.springSpeed = 3;
        anim.beginTime = CACurrentMediaTime() +  [time[i] doubleValue]*1.5;
        [btn pop_addAnimation:anim forKey:nil];
        
    }
    
}

- (IBAction)back:(id)sender {
    NSArray *time = @[ @0.30,@0.25,@0.15,@0.20,@0.1,@0,@0.05];
    for (int i = 0; i< self.view.subviews.count ; i ++) {
        if (i > 1) {
            UIView *view = self.view.subviews[i];
            POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
            anim.fromValue = [NSValue valueWithCGRect:view.frame];
            anim.toValue = [NSValue valueWithCGRect:CGRectMake(view.frame.origin.x, view.frame.origin.y + [UIScreen mainScreen].bounds.size.height, view.frame.size.width, view.frame.size.height)];
            [view pop_addAnimation:anim forKey:nil];
            anim.duration = 0.6;
            anim.beginTime = [time[(i - 2)] doubleValue] + CACurrentMediaTime();
            
            if (i == 2 ) {
                [anim setCompletionBlock:^(POPAnimation *anim, BOOL complete) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
            }

        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
